package cn.edu.bupt.msorder.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/order")
public class OrderController {
    private final RestTemplate restTemplate;

    public OrderController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/product/{product_uuid}/{num}")
    public String order(@PathVariable("product_uuid") String product_uuid,
                         @PathVariable("num") Integer num,
                         HttpServletRequest request
    ){
        String REST_SERVICE_URL_PREFIX = "http://ms-inventory";
        String inventoryServiceUrl = REST_SERVICE_URL_PREFIX + "/product/deduct/"
                + product_uuid + "/" + num.toString();
        String result = restTemplate.getForObject(inventoryServiceUrl, String.class);
        return result;
    }
}
